import java.util.Scanner;

public class demo04 {
    //剪格子
    public static int sum;
    public static int min;
    public static int[] di={1,0,0,-1};
    public static int[] dj={0,-1,1,0};
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int[][] arr=new int[n][m];
        sum=0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j]=sc.nextInt();
                sum+=arr[i][j];
            }
        }
        min=Integer.MAX_VALUE;
        int[][] num=new int[n][m];
        dfs(arr,0,0,0,1,num);
        System.out.println(min);
    }
    public static void dfs(int[][] arr,int i,int j,int n,int len,int[][] num){
        n+=arr[i][j];
        num[i][j]=1;
        if(n*2==sum){
            min=Math.min(min,len);
            return;
        } else if (n*2>sum) {
            return;
        }
        for (int k = 0; k < 4; k++) {
            int i1=i+di[k];
            int j1=j+dj[k];
            if(i1>=0&&i1<arr.length&&j1>=0&&j1<arr[i1].length){
                if(num[i1][j1]==0){
                    dfs(arr,i1,j1,n,len+1,num);
                    num[i1][j1]=0;
                }
            }
        }
    }
}
